Fourier avec Maxima

Approximation en série de Fourier

Nous rappelons que, pour une fonction \(f:\mathbb{R}\to\mathbb{R}\) continue par morceau de période \(T\), les coefficients de Fourier sont les suivants :

\[\begin{eqnarray*} a_0 &=& \frac{1}{T}\cdot\int_{-\frac{T}{2}}^{\frac{T}{2}}f(x)\,dx \\ a_n &=& \frac{2}{T}\cdot\int_{-\frac{T}{2}}^{\frac{T}{2}}f(x)\cdot \cos\left(\frac{2\pi}{T}nx\right)\,dx \\ b_n &=& \frac{2}{T}\cdot\int_{-\frac{T}{2}}^{\frac{T}{2}}f(x)\cdot \sin\left(\frac{2\pi}{T}nx\right)\,dx \\ c_0 &=& a_0 \\ c_n &=& \frac{2}{T}\cdot\int_{-\frac{T}{2}}^{\frac{T}{2}}f(x)\cdot \exp\left(-\frac{2\pi i}{T}nx\right)\,dx \\ \end{eqnarray*}\]

La fonction \(f\) s’écrit alors comme

\[ f(x)=a_0+\sum_{k=-\infty}^\infty \left[a_k\cdot\cos\left(\frac{2\pi}{T}kx\right)+b_k\cdot\sin\left(\frac{2\pi}{T}kx\right)\right]. \]

knitr::opts_chunk$set(echo = TRUE)
options(digits=2)
library(xtable)
library(rim)
library(reticulate)  ## better Python in R
maxima.options(engine.format = "latex", 
           engine.label = TRUE,
           inline.format = "latex", 
           inline.label = FALSE)
(%i1) maxima_userdir: "C:/maxima-5.46.0/Maxima/" $
(%i2) maxima_usertemp: "C:/maxima-5.46.0/Maxima/" $
(%i3) file_search_maxima : append(["C:/maxima-5.46.0/Maxima/###.{mac,mc}"],file_search_maxima )$
(%i4) file_search_lisp : append(["C:/maxima-5.46.0/Maxima/###.{mac,mc}"],file_search_lisp )$
(%i5) maxima.load(mbe1util)$
(%i6) maxima.load(qfft)$
(%i7) maxima.load(fourie)$  
(%i8) maxima.load(mfiles)$
(%i9) inpart(x+y,0)

\[\mathtt{(\textit{%o}_{9})}\quad \mbox{ + }\] The way to work with maxima in a R-chunk:

maxima.get("1+1")
## (%o1) 2

Cas des signaux définis analytiquement par morceau

Développement en série de Fourier pour une période p quelconque (Maxima by Example: Ch.10, p. 7 et suivantes) : quelques exemples pour des signaux définis par une structure conditionnelle if-then-else, comme par ex. un signal carré.

Fonction valeur absolue

We define the function to have period 4 with f(x)=|x| for -2<=x<=2. This function is an even function of x so the sin coefficients b_n are all zero. The average value of |x| over the domain [-2,2] (moyenne qui est bien sûr égale au coefficient a_0) is greater than zero so we will have a non-zero coefficient a_0 which we will calculate separately. We do this calculation by hand here:

(%i10) integrate(abs(x)*cos(n*%pi*x/2),x,-2,2)/2;

\[\mathtt{(\textit{%o}_{10})}\quad \frac{\int_{-2}^{2}{\left| x\right| \,\cos \left(\frac{\pi\,n\,x}{2}\right)\;dx}}{2}\]

The function integrate cannot cope with abs(x), so we have to split up the region of integration:

(%i11) (declare(n,integer),assume(n > 0), facts() );

\[\mathtt{(\textit{%o}_{11})}\quad \left[ \textit{kind}\left(n , \textit{integer}\right) , n>0 \right] \]

(%i12) a0 :integrate(-x,x,-2,0)/2 + integrate(x,x,0,2)/2;

\[\mathtt{(\textit{%o}_{12})}\quad 2\]

(%i13) an : integrate((-x)*cos(n*%pi*x/2),x,-2,0)/2 + integrate(x*cos(n*%pi*x/2),x,0,2)/2;

\[\mathtt{(\textit{%o}_{13})}\quad \frac{4\,\left(-1\right)^{n}}{\pi^2\,n^2}-\frac{4}{\pi^2\,n^2}\]

(%i14) an : (ratsimp(an), factor(%%) );

\[\mathtt{(\textit{%o}_{14})}\quad \frac{4\,\left(\left(-1\right)^{n}-1\right)}{\pi^2\,n^2}\]

(%i15) define(a(n),an);

\[\mathtt{(\textit{%o}_{15})}\quad a\left(n\right):=\frac{4\,\left(\left(-1\right)^{n}-1\right)}{\pi^2\,n^2}\]

(%i16) fs(nmax) := a0/2 + sum(a(m)*cos(m*%pi*x/2),m,1,nmax);

\[\mathtt{(\textit{%o}_{16})}\quad \textit{fs}\left(\textit{nmax}\right):=\frac{a_{0}}{2}+\textit{sum}\left(a\left(m\right)\,\cos \left(\frac{m\,\pi\,x}{2}\right) , m , 1 , \textit{nmax}\right)\]

(%i17) define(fss(nmax,x),fs(nmax));

\[\mathtt{(\textit{%o}_{17})}\quad \textit{fss}\left(\textit{nmax} , x\right):=\frac{4\,\sum_{m=1}^{\textit{nmax}}{\frac{\left(\left(-1\right)^{m}-1\right)\,\cos \left(\frac{\pi\,m\,x}{2}\right)}{m^2}}}{\pi^2}+1\]

From Maxima…

(%i18) y2 : makelist(fss(5,i),i,-2,2,0.1)$
(%i19) ev(y2,sum)$

Here we meet an important trick in order to force Maxima to evaluate the sum in the expression of y2. To understand the point, first try the formula ev(y2)$ instead of ev(y2,sum)$. Explanations:

Maxima represents partially-evaluated expressions as so-called noun expressions. Many mathematical functions return noun expressions when the arguments aren’t specific enough to permit complete evaluations. sum, integrate, diff, limit, etc. return noun expressions; programming functions such as length, first, op, etc., complain with an error message if arguments aren’t specific enough.

Nouns can be “verbified” via ev(expr, nouns). You can also nounify just one operator (if there’s more than one nounified expression present) by saying the name of the operator, e.g. ev(expr, sum) to verbify just sum.

After this important technical explanation, we go forth with our calculations. We are about to pass the variable \(y2\) from Maxima to R by the help of a file variable which we’ll call “foo”:

From Maxima…

(%i20) float(%)

\[\mathtt{(\textit{%o}_{20})}\quad \left[ 1.9331 , 1.9038 , 1.8238 , 1.7134 , 1.5955 , 1.4865 , 1.3908 , 1.302 , 1.21 , 1.1088 , 1.0 , 0.89116 , 0.78996 , 0.69804 , 0.60921 , 0.51345 , 0.40449 , 0.28661 , 0.17616 , 0.096237 , 0.066944 , 0.096237 , 0.17616 , 0.28661 , 0.40449 , 0.51345 , 0.60921 , 0.69804 , 0.78996 , 0.89116 , 1.0 , 1.1088 , 1.21 , 1.302 , 1.3908 , 1.4865 , 1.5955 , 1.7134 , 1.8238 , 1.9038 , 1.9331 \right] \]

…to R:

foo
## $o20
## list(1.933055522253, 1.90376334567521, 1.82383523996963, 1.71338531872297, 
##     1.59551061454633, 1.48654844949286, 1.39078551597002, 1.30196275910785, 
##     1.2100398003337, 1.10883949708292, 0.999999999999999, 0.891160502917079, 
##     0.7899601996663, 0.698037240892147, 0.609214484029983, 0.513451550507135, 
##     0.404489385453673, 0.286614681277026, 0.17616476003037, 0.0962366543247846, 
##     0.066944477747005, 0.0962366543247853, 0.176164760030372, 
##     0.286614681277028, 0.404489385453675, 0.513451550507136, 
##     0.609214484029984, 0.698037240892148, 0.789960199666302, 
##     0.89116050291708, 1, 1.10883949708292, 1.2100398003337, 1.30196275910785, 
##     1.39078551597002, 1.48654844949287, 1.59551061454633, 1.71338531872297, 
##     1.82383523996963, 1.90376334567522, 1.933055522253)

foo is a text value which has to be copied below without “##”:

y2 <-
 list(1.933055522253, 1.90376334567521, 1.82383523996963, 1.71338531872297, 
     1.59551061454633, 1.48654844949286, 1.39078551597002, 1.30196275910785, 
     1.2100398003337, 1.10883949708292, 0.999999999999999, 0.891160502917079, 
     0.7899601996663, 0.698037240892147, 0.609214484029983, 0.513451550507135, 
     0.404489385453673, 0.286614681277026, 0.17616476003037, 0.0962366543247846, 
     0.066944477747005, 0.0962366543247853, 0.176164760030372, 
     0.286614681277028, 0.404489385453675, 0.513451550507136, 
     0.609214484029984, 0.698037240892148, 0.789960199666302, 
     0.89116050291708, 1, 1.10883949708292, 1.2100398003337, 1.30196275910785, 
     1.39078551597002, 1.48654844949287, 1.59551061454633, 1.71338531872297, 
     1.82383523996963, 1.90376334567522, 1.933055522253)

qdraw( ex([abs(x),fs(5) ],x,-2,2),key(bottom) )$

#define data to plot
x <- seq(-2,2,by=0.1)
y1 <- abs(x)
plot(x, y1, type='l', col='blue', xlab='x', ylab='y')
#add second line to plot
lines(x, y2, col='red')

Symbolic calculus
SymPy: a serious concurrent for Maxima?

import math
math.sqrt(3)
## 1.7320508075688772
import sympy
sympy.sqrt(3)
## sqrt(3)
from sympy import symbols
x,y = symbols('x y')
expr = x+2*y
expr
## x + 2*y
from sympy import expand, factor
expanded_expr = expand(x*expr)
expanded_expr
## x**2 + 2*x*y
factor(expanded_expr)
## x*(x + 2*y)

The printing

from sympy import *
init_printing(use_unicode=True) # allow LaTeX printing
x,t,z,nu = symbols('x t z nu')
expr1 = diff(sin(x)*exp(x),x)
expr2 = Integral(sqrt(1/x),x)
expr1
##  x           x       
## ℯ ⋅sin(x) + ℯ ⋅cos(x)
expr2
## ⌠           
## ⎮     ___   
## ⎮    ╱ 1    
## ⎮   ╱  ─  dx
## ⎮ ╲╱   x    
## ⌡
print_latex(expr1)
## e^{x} \sin{\left(x \right)} + e^{x} \cos{\left(x \right)}

\[ e^{x} \sin{\left(x \right)} + e^{x} \cos{\left(x \right)} \]

print_latex(expr2)
## \int \sqrt{\frac{1}{x}}\, dx

\[ \int \sqrt{\frac{1}{x}}\, dx \]